package com.duolaibei.business.view.service;

/**
 * Created by xlm on 2017/8/8 0008
 */

import android.content.Context;
import android.os.Message;
import android.util.Log;

import com.duolaibei.business.view.App;
import com.igexin.sdk.GTIntentService;
import com.igexin.sdk.message.GTCmdMessage;
import com.igexin.sdk.message.GTTransmitMessage;

/**
 * 继承 GTIntentService 接收来自个推的消息, 所有消息在线程中回调, 如果注册了该服务, 则务必要在 AndroidManifest中声明, 否则无法接受消息<br>
 * onReceiveMessageData 处理透传消息<br>
 * onReceiveClientId 接收 cid <br>
 * onReceiveOnlineState cid 离线上线通知 <br>
 * onReceiveCommandResult 各种事件处理回执 <br>
 */
public class DlbIntentService extends GTIntentService {

    public DlbIntentService() {
    }

    @Override
    public void onReceiveServicePid(Context context, int pid) {

    }

    @Override
    public void onReceiveMessageData(Context context, GTTransmitMessage msg) {
        byte[] payload = msg.getPayload();
        if (payload != null) {
            String data = new String(payload);
            sendMessage(data, 0);
//            if (SysUtil.isAppAlive(context, "com.duolaibei.business")) {//已经启动
//                sendMessage(data, 0);
//            } else {
//                //未启动
//                Intent launchIntent = context.getPackageManager().
//                        getLaunchIntentForPackage(getPackageName());
//                PreferenceUtil.setParam(Preferences.LAUNCHINTENT, "launchIntent");
//                launchIntent.setFlags(
//                        Intent.FLAG_ACTIVITY_NEW_TASK |
//                                Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
//                context.startActivity(launchIntent);
//            }
            Log.i("TAG", "---------data-------" + data);
        }
    }

    @Override
    public void onReceiveClientId(Context context, String clientid) {
        if (clientid != null) {
            sendMessage(clientid, 1);
            Log.i("TAG", "---------clientid-------" + clientid);
        }
    }

    @Override
    public void onReceiveOnlineState(Context context, boolean online) {
    }

    @Override
    public void onReceiveCommandResult(Context context, GTCmdMessage cmdMessage) {
    }

    private void sendMessage(String data, int what) {
        Message msg = Message.obtain();
        msg.what = what;
        msg.obj = data;
        App.sendMessage(msg);
    }
}
